/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VistaSeccion.java
 *
 * Created on 14/02/2011, 01:20:46 AM
 */
package Matricula_Nota;

import java.util.ArrayList;
import javax.swing.DefaultListSelectionModel;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author jaime guerrero
 * @author Cambio : Pedro A. Anaya Avila
 */
public class VistaNotas extends javax.swing.JDialog {

    private Coordinador coordinador;
    private DefaultTableModel mdlAlumnos;
    private ArrayList<Seccion> secciones;

    /** Creates new form VistaSeccion */
    public VistaNotas(java.awt.Frame parent, boolean modal, Coordinador coordinador) {
        super(parent, modal);
        initComponents();
        this.coordinador = coordinador;
        mdlAlumnos = (DefaultTableModel) tblAlumnos.getModel();
        tblAlumnos.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
        secciones = new ArrayList<Seccion>();
    }

    private void mostrarAlumnos() {
        mdlAlumnos.setRowCount(secciones.get(cboSeccion.getSelectedIndex()).getAlumnos().size());
        for (int i = 0; i < secciones.get(cboSeccion.getSelectedIndex()).getAlumnos().size(); i++) {
            Alumno alumno = secciones.get(cboSeccion.getSelectedIndex()).getAlumnos().get(i);
            mdlAlumnos.setValueAt(alumno.getCodigo(), i, 0);
            mdlAlumnos.setValueAt(alumno.getNombre(), i, 1);
            mdlAlumnos.setValueAt(coordinador.obtenerCantidadCursosAprobados(alumno), i, 2);
            mdlAlumnos.setValueAt(coordinador.obtenerCantidadCursosDesaprobados(alumno), i, 3);
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        cboSeccion = new javax.swing.JComboBox();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        txtPeriodo = new javax.swing.JTextField();
        btnBuscarSeccion = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblAlumnos = new javax.swing.JTable();
        btnMostrarCalificacion = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        txtPromedioSeccion = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtPromovidos = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        txtRepitentes = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Registro de Notas");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Opciones de Busqueda"));

        cboSeccion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboSeccionActionPerformed(evt);
            }
        });

        jLabel13.setText("Sección");

        jLabel14.setText("Año");

        btnBuscarSeccion.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Matricula_Nota/Recursos/Find.png"))); // NOI18N
        btnBuscarSeccion.setToolTipText("Buscar Secciones");
        btnBuscarSeccion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarSeccionActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel14)
                    .addComponent(jLabel13))
                .addGap(40, 40, 40)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtPeriodo, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnBuscarSeccion, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(cboSeccion, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(391, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtPeriodo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel14))
                    .addComponent(btnBuscarSeccion))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cboSeccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel13))
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Lista de Alumnos"));

        tblAlumnos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Codigo", "Nombre y Apellido", "Cursos Aprobados", "Cursos Desaprobados"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblAlumnos);

        btnMostrarCalificacion.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Matricula_Nota/Recursos/List.png"))); // NOI18N
        btnMostrarCalificacion.setToolTipText("Calificar");
        btnMostrarCalificacion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMostrarCalificacionActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 559, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnMostrarCalificacion, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnMostrarCalificacion)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Estadisticas"));

        jLabel6.setText("Promedio Sección");

        txtPromedioSeccion.setEditable(false);
        txtPromedioSeccion.setText("00.00");

        jLabel7.setText("Promovidos");

        txtPromovidos.setEditable(false);
        txtPromovidos.setText("00.00");

        jLabel8.setText("Repitentes");

        txtRepitentes.setEditable(false);
        txtRepitentes.setText("00.00");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(txtPromovidos, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel8)
                .addGap(28, 28, 28)
                .addComponent(txtRepitentes, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txtPromedioSeccion, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(141, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE, false)
                .addComponent(jLabel6)
                .addComponent(txtPromedioSeccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel7)
                .addComponent(txtPromovidos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel8)
                .addComponent(txtRepitentes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, 612, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnMostrarCalificacionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMostrarCalificacionActionPerformed
        int fila = tblAlumnos.getSelectedRow();
        if (fila != -1) {
            String codigoAlumno = mdlAlumnos.getValueAt(fila, 0).toString();
            Seccion seccion = secciones.get(cboSeccion.getSelectedIndex());
            VistaCalificacion vista = new VistaCalificacion(this, true, seccion, codigoAlumno, txtPeriodo.getText(), coordinador);
            vista.setLocationRelativeTo(this);
            vista.setVisible(true);
            mostrarAlumnos();
            mostrarEstadisticas();
        }
}//GEN-LAST:event_btnMostrarCalificacionActionPerformed

    private void btnBuscarSeccionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarSeccionActionPerformed
        ArrayList<Seccion> secciones2 = coordinador.buscarSeccionPeriodo(Integer.parseInt(txtPeriodo.getText().trim()));
        cboSeccion.removeAllItems();
        this.secciones.clear();
        for (Seccion seccion : secciones2) {
            this.secciones.add(seccion);
            cboSeccion.addItem(seccion.getNombre());
        }
    }//GEN-LAST:event_btnBuscarSeccionActionPerformed

    private void cboSeccionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboSeccionActionPerformed
        mostrarAlumnos();
    }//GEN-LAST:event_cboSeccionActionPerformed

    private void mostrarEstadisticas() {
        double promedioSalon = coordinador.calcularPromedioSeccion(secciones.get(cboSeccion.getSelectedIndex()));
        txtPromedioSeccion.setText(String.valueOf(Math.round(promedioSalon)));
        int cantDesapro = 0;
        for (int i = 0; i < mdlAlumnos.getRowCount(); i++) {
            if (Integer.parseInt(mdlAlumnos.getValueAt(i, 3).toString().trim()) == 3) {
                cantDesapro++;
            }
        }
        txtRepitentes.setText(String.valueOf(cantDesapro));
        txtPromovidos.setText(String.valueOf(mdlAlumnos.getRowCount() - cantDesapro));
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscarSeccion;
    private javax.swing.JButton btnMostrarCalificacion;
    private javax.swing.JComboBox cboSeccion;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblAlumnos;
    private javax.swing.JTextField txtPeriodo;
    private javax.swing.JTextField txtPromedioSeccion;
    private javax.swing.JTextField txtPromovidos;
    private javax.swing.JTextField txtRepitentes;
    // End of variables declaration//GEN-END:variables
}
